package com.MoveEat.views;

import org.osmdroid.api.IGeoPoint;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ScaleBarOverlay;

import android.app.Activity;
import android.view.View;
import android.widget.Gallery;

import com.MoveEat.Utils.MySimpleLocationOverlay;
import com.MoveEat.Utils.ResourceProxyImpl;

public class RestMap
{
	// private OpenStreetMapViewController mMapController;
	// private MyLocationOverlay mMyLocationOverlay;
	private MapView			mOSMV;
	private Activity		mContext;
	private String			mAddress;
	private MapController	mMapController;
	private ScaleBarOverlay	mScaleBarOverlay;
	private MySimpleLocationOverlay	mRestLocationOverlay;
	private GeoPoint	mRestGeoPoint;

	public RestMap(Activity context, GeoPoint geopoint, String address)
	{

		mContext = context;
		mAddress = address;
		mRestGeoPoint = geopoint;
		mOSMV = new MapView(context,null);
		mOSMV.setLayoutParams(new Gallery.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
		mOSMV.setTileSource(TileSourceFactory.MAPNIK);
		mOSMV.setBuiltInZoomControls(true);
		mOSMV.setClickable(true);
		mOSMV.setMultiTouchControls(true);
		mOSMV.setEnabled(false);
		mMapController = mOSMV.getController();
		mMapController.setZoom(17);

		mMapController.setCenter(geopoint);

		// mMyLocationOverlay = new MyLocationOverlay(context, mOSMV,
		// mResourceProxy);
		// mMyLocationOverlay.setLocation(geopoint);
		// mOSMV.getOverlayManager().add(mMyLocationOverlay);
		 mScaleBarOverlay = new ScaleBarOverlay(context);
		 mOSMV.getOverlays().add(mScaleBarOverlay);
		 mRestLocationOverlay = new MySimpleLocationOverlay(mContext, new ResourceProxyImpl(mContext),mAddress);
		 
		 mRestLocationOverlay.setLocation(geopoint);
		 mRestLocationOverlay.onSingleTapUp(null, mOSMV);
		 mOSMV.getOverlays().add(mRestLocationOverlay);
		// mLayout.addView(mOSMV, new
		// RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
		// LayoutParams.FILL_PARENT));
		// setMapCenter(geopoint);

	}

	// public void setMapCenter(GeoPoint geoPoint)
	// {
	// final org.andnav.osm.util.Point p;
	//
	// if (geoPoint != null)
	// p = Mercator.projectGeoPoint(geoPoint, getPixelZoomLevel(), null);
	// else
	// p = Mercator.projectGeoPoint(mMyLocationOverlay.getMyLocation(),
	// getPixelZoomLevel(), null);
	//
	// final int worldSize_2 = this.getWorldSizePx() / 2;
	// mOSMV.scrollTo(p.x - worldSize_2, p.y - worldSize_2);
	// }

	// int getWorldSizePx()
	// {
	// return (1 << getPixelZoomLevel());
	// }
	//
	// int getPixelZoomLevel()
	// {
	// return mOSMV.getZoomLevel() + mOSMV.getRenderer().MAPTILE_ZOOM;
	// }

	public View getView()
	{
		return mOSMV;
	}

	public IGeoPoint getRestLocation()
	{
		return mRestGeoPoint;
	}
}
